AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )   
include("shared.lua")

function ENT:Initialize( )
 
	self:SetModel( "models/odessa.mdl" )
	self:SetHullType( HULL_HUMAN )
	self:SetHullSizeNormal( )
	self:SetNPCState( NPC_STATE_SCRIPT )
	self:SetSolid( SOLID_BBOX )
	self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD )
	self:SetUseType( SIMPLE_USE )
	self:DropToFloor()
 
	self:SetMaxYawSpeed( 90 )
 
end

local NextTime = 0

function ENT:AcceptInput( name, activator, caller, data )

    if (CurTime() >= NextTime) then
	        NextTime = CurTime() +  1

    if ( name == "Use" && ValidEntity( activator ) && activator:IsPlayer( ) ) then
		umsg.Start("CarDealerUsed", activator)
		umsg.End()
		end
	end
end

concommand.Add( "_buycar_", function( ply, cmd, args )
	local trace = ply:GetEyeTrace()
	local filename = "Lane101/Cars/"..ply:UniqueID().."/OwnedCars.txt"
	local f = file.Read( filename )
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "car_dealer_npc" then
	
		local CarsName = string.Explode( "\n", f )
		for k,v in pairs( CarsName ) do
			if v == args[1] then
				ply:SendLua("GAMEMODE:AddNotify(\"You already own this car!\", NOTIFY_ERROR, 5)")
				--break
				return
			end
		end
	
		if ply:CanAfford( args[2] ) then
		
			ply:SendLua("GAMEMODE:AddNotify(\"You have purchased a "..args[1].."!\", NOTIFY_ERROR, 5)")
			--ply:AddMoney( -args[2] )
		
			if file.Exists( filename ) then
				filex.Append( filename, "\n"..args[1] )
			else
				file.Write( filename, args[1] )
			end
			
		else 
			
			ply:SendLua("GAMEMODE:AddNotify(\"You cannot afford this!\", NOTIFY_ERROR, 5)")
			
		end
	end
end)

concommand.Add( "_spawncar_", function( ply, cmd, args )

	local trace = ply:GetEyeTrace()
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "car_dealer_npc" then
	
		local f = file.Read( "Lane101/Cars/"..ply:UniqueID().."/OwnedCars.txt" )
                if !f then
	
			ply:SendLua("GAMEMODE:AddNotify(\"You don't have this car!\", NOTIFY_ERROR, 5)")
		
		else
	
			local CarsName = string.Explode( "\n", f )
			
		if ply.Withdrawn then
			ply:SendLua("GAMEMODE:AddNotify(\"You already have your car spawned!\", NOTIFY_ERROR, 5)")
			return
		
		else
			
			for k,v in pairs( CarsName ) do
				if v == args[1] then
				
					local Vehicle = ents.Create( "prop_vehicle_jeep_old" )
					Vehicle:SetModel( args[2] )
					Vehicle:SetKeyValue( "vehiclescript", args[3] )
					Vehicle:SetPos( Vector( 470.339813, -21.209581, -84.123688 ) )
					Vehicle:Spawn()
					Vehicle:Activate()
					ply.Withdrawn = true
					--Vehicle:Own(ply)
					ply:SendLua("GAMEMODE:AddNotify(\"You have spawned your car!\", NOTIFY_ERROR, 5)")
					ply.CurrentCar = Vehicle
					
					break
						
					end
				end
			end
		end
	end
end)

concommand.Add( "_removecar_", function( ply, cmd, args )

	local trace = ply:GetEyeTrace()
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "car_dealer_npc" then
	
		if ply.CurrentCar and ValidEntity( ply.CurrentCar ) then
			ply.CurrentCar:Remove( "prop_vehicle_jeep_old" )
			ply.Withdrawn = false
		end
	end
end)